package com.gdcp.service.impl;

import com.gdcp.beans.RoleUser;
import com.gdcp.beans.User;
import com.gdcp.common.ResponseModel;
import com.gdcp.mapper.RoleUserMapper;
import com.gdcp.mapper.UserMapper;
import com.gdcp.service.RoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Service
public class RoleUserServiceImpl implements RoleUserService {

    @Autowired
    RoleUserMapper roleUserMapper;
    @Autowired
    ResponseModel responseModel;
    @Autowired
    UserMapper userMapper;

    @Override
    public int add(int user_id, int role_id) {
        RoleUser roleUser=new RoleUser();
        roleUser.setIuser_id(user_id);
        roleUser.setIrole_id(role_id);
        try{
            roleUserMapper.add(roleUser);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public Map<String, Object> findPageBreakByCondition(Map<String, Object> model) {
        Map<String,Object> result = new HashMap<>();
        int total=roleUserMapper.findPageBreakByConditionByShopId().size();
        List<RoleUser> rows=roleUserMapper.findPageBreakByCondition(model);
        if (rows.size()==0){
            int count= (int) model.get("a");
            count=count-(int) model.get("b");
            if (count>=0) {
                model.put("a", count);
                rows = roleUserMapper.findPageBreakByCondition(model);
            }
        }
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    @Transactional(rollbackFor = { Exception.class })
    public Map<String, Object> removeByPrimaryKey(Integer[] ids) {
        try{
            for (Integer id:ids) {
                roleUserMapper.removeByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return responseModel.setModel(false,"删除["+ids.length+"条记录失败"+e.toString());
        }

        return responseModel.setModel(true,"成功删除["+ids.length+"条记录");
    }

    @Override
    public Map<String,Object> updateInformation(RoleUser roleUser) {
        try{
            roleUserMapper.updateInformation(roleUser);
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false, "权限修改失败");
        }
        return responseModel.setModel(true, "修改成功");
    }

    @Override
    public RoleUser getInformation(Integer id) {
        return roleUserMapper.getInformation(id);
    }

    @Override
    public Map<String, Object> searchInformation(Map<String, Object> model) {
        Map<String,Object> result = new HashMap<>();
        int total=roleUserMapper.searchInformation(null).size();
        List<RoleUser> rows=roleUserMapper.searchInformation(model);

        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    public RoleUser getInformationByUserId(Integer id) {
        return roleUserMapper.getInformationByUserId(id);
    }
}
